//
//  Mu_BaseViewController.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/10.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1BaseViewController.h"
#import "UINavigationController+M1StatusBar.h"
@interface M1BaseViewController ()

@end

@implementation M1BaseViewController

-(instancetype)init{
    self = [super init];
    if (self) {
        self.m1_hiddenNav = NO;
        self.m1_changeNavTitleColor = NO;
        self.m1_changeBackImage = NO;
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.view endEditing:YES];
    
    if (self.navigationController) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController setNavigationBarHidden:self.m1_hiddenNav animated:animated];
      
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:m1_boldFont(16),NSForegroundColorAttributeName:self.m1_changeNavTitleColor?[UIColor whiteColor]:[UIColor m1_colorHex:0x333333]}];
        
        
    }
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor m1_colorHex:0xF6F8FB];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    if (self.navigationController.viewControllers.count > 1) {
        _m1_backBtn = [[UIBarButtonItem alloc]initWithImage:(_m1_changeBackImage?[[UIImage imageNamed:@"white_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]:[[UIImage imageNamed:@"black_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]) style:UIBarButtonItemStylePlain target:self action:@selector(m1_back)];
        self.navigationItem.leftBarButtonItem = _m1_backBtn;
    }
}
-(UIView *)m1_creatNavWithTitle:(NSString *)title{
    UIView *m1_nav = [[UIView alloc]init];
    m1_nav.backgroundColor = m1_bg_color;
    m1_nav.frame = CGRectMake(0, 0, SCREENHEIGHT, m1_NAV_HEIGHT);
    [self.view addSubview:m1_nav];
    
    UIButton *m1_backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    [m1_backItem setImage:[[UIImage imageNamed:@"black_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:0];
    m1_backItem.frame = CGRectMake(0, iPhoneX?44:20, 60, 44);
    [m1_backItem addTarget:self action:@selector(m1_back) forControlEvents:UIControlEventTouchUpInside];
    [m1_nav addSubview:m1_backItem];
    
    UILabel *m1_titleLB = [[UILabel alloc]init];
    m1_titleLB.font = m1_boldFont(17);
    m1_titleLB.text =  title;
    m1_titleLB.textAlignment = NSTextAlignmentCenter;
    m1_titleLB.frame = CGRectMake(70, iPhoneX?44:20, SCREENWIDTH-140, 44);
    m1_titleLB.textColor = [UIColor m1_colorHex:0x333333];
    [m1_nav addSubview:m1_titleLB];
    
    return m1_nav;
}
-(void)m1_banPopGesture{
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
}
-(void)m1_back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
    
    //返回路径中的文件数组
    NSArray*files = [[NSFileManager defaultManager]subpathsAtPath:cachePath];
    
    for(NSString *p in files){
        NSError*error;
        
        NSString*path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
        
        if([[NSFileManager defaultManager]fileExistsAtPath:path])
        {
            [[NSFileManager defaultManager]removeItemAtPath:path error:&error];
            
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
